<template>
  <div class="login-background">
    <div class="header-container">
      <h1 class="header-title">VC-KD</h1>
      <h2 class="header-explain">Victor's Knowledge Domain Generation Site</h2>
    </div>
    <div class="login-container" @mouseleave="handleMouseLeaveContainer">
      <div
          class="left-container"
          @mouseover="handleMouseOver('left')"
          @mouseleave="handleMouseLeave('left')"
      >
        <div v-if="isMouseOverLeft">
          <div class="title" style="font-size: 40px">
            <span>登录</span>
          </div>
          <div class="input-container">
            <input type="text" v-model="email" style="font-size: 20px;" placeholder="用户邮箱">
            <br>
            <input type="password" v-model="password" style="font-size: 20px;" placeholder="密码">
          </div>
          <VerificationCode @verify="handleVerification"/>
          <el-button class="custom-button" @click="loginHandler">提交</el-button>
        </div>
      </div>
      <div
          class="right-container"
          @mouseover="handleMouseOver('right')"
          @mouseleave="handleMouseLeave('right')"
      >
        <div v-if="isMouseOverRight">
          <div class="title" style="font-size: 40px">
            <span>注册</span>
          </div>
          <div class="input-container">
            <input type="text" v-model="emailR" style="font-size: 20px;" placeholder="用户邮箱">
            <br>
            <input type="password" v-model="password1" style="font-size: 20px;" placeholder="密码">
            <br>
            <input type="password" v-model="password2" style="font-size: 20px;" placeholder="再次输入密码">
          </div>
          <el-button class="custom-button" @click="registerHandler">完成注册</el-button>
        </div>
      </div>
    </div>
  </div>
</template>


<script setup>
import {useRouter} from 'vue-router';
import {ref} from 'vue';
import VerificationCode from '@/components/verificationCode.vue';
import { login, createUser } from '@/api/user';
import {ElLoading, ElNotification} from "element-plus";
// 默认显示登录界面
const isMouseOverLeft = ref(true);
const isMouseOverRight = ref(false);
// 注册密码校验
const password1 = ref('')
const password2 = ref('')
// 登录密码校验
const email = ref('');
const emailR = ref('')
const password = ref('');
// 验证码校验
const isCodeValid = ref(false);
// 使用路由
const router = useRouter();
const handleVerification = ({ valid }) => {
  isCodeValid.value = valid;
};

// 定义 navigateTo 方法
const navigateTo = (route) => {
  const loadingInstance = ElLoading.service();
  setTimeout(() => {
    router.push(route);
    loadingInstance.close();
  }, 1000);
};

// 更新后的 loginHandler 方法
const loginHandler = async () => {
  if (!isCodeValid.value) {
    ElNotification({
      title: '错误',
      message: '验证码无效',
      type: 'error',
    });
    return;
  }
  try {
    const response = await login({ email: email.value, password: password.value });
    if (response.code === 200) {
      // 登录成功
      const userData = {
        id: response.id,
        email: response.email,
        username: response.username,
        role: response.role,
      };
      // 存储用户信息到本地存储
      localStorage.setItem('user', JSON.stringify(userData));
      console.log(localStorage)
      // 跳转到主页
      navigateTo({ path: '/home' });
      ElNotification({
        title: '成功',
        message: '登录成功',
        type: 'success'
      });
    } else {
      // 登录失败
      ElNotification({
        title: '登录失败',
        message: response.msg || '请检查邮箱和密码',
        type: 'error'
      });
    }
  } catch (err) {
    // 处理错误
    ElNotification({
      title: '登录失败',
      message: '登录失败，请检查邮箱和密码是否正确',
      type: 'error'
    });
    console.error('登录失败', err);
  }
};

const registerHandler = async () => {

  if (password1.value !== password2.value) {
    ElNotification({
      title: '错误',
      message: '两次输入密码不一致',
      type: 'error'
    });
    return;
  }

  try {
    const response = await createUser({email: emailR.value, password: password1.value});
    if (response.code === 200) {
      // 跳转到主页
      window.location.reload();
      ElNotification({
        title: '成功',
        message: '注册成功',
        type: 'success'
      });
    } else {
      // 登录失败
      ElNotification({
        title: '注册失败',
        message: response.msg || '请检查邮箱和密码',
        type: 'error'
      });
    }
  } catch (err) {
    // 处理错误
    ElNotification({
      title: '注册失败',
      message: '登录失败，请检查邮箱和密码格式是否正确',
      type: 'error'
    });
    console.error('注册失败', err);
  }


};

const handleMouseOver = (container) => {
  if (container === 'left') {
    isMouseOverLeft.value = true;
    isMouseOverRight.value = false;
  } else if (container === 'right') {
    isMouseOverLeft.value = false;
    isMouseOverRight.value = true;
  }
};

const handleMouseLeave = (container) => {
  if (container === 'left') {
    isMouseOverLeft.value = false;
  } else if (container === 'right') {
    isMouseOverRight.value = false;
  }
};

// 处理整个容器的鼠标离开事件，恢复默认状态
const handleMouseLeaveContainer = () => {
  isMouseOverLeft.value = true;
  isMouseOverRight.value = false;
};

</script>


<style scoped>

@import url('https://fonts.googleapis.com/css2?family=Pacifico&display=swap');

/* 顶部容器样式 */
.header-container {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 0;
}

.header-title {
  color: white;
  font-size: 70px;
  font-family: "Pacifico", cursive;
  font-weight: 400;
  font-style: normal;
}

.header-explain {
  margin-top: 50px;
  color: white;
  font-size: 20px;
  font-family: "Pacifico", cursive;
  font-weight: 400;
  font-style: normal;
}

.custom-button {
  width: 100%;
  height: 50px;
  border: none;
  font-size: 20px;
  background-color: rgb(114, 135, 254);
  color: white;
  margin-top: 20px;
}

.custom-button:hover {
  background-color: rgb(130, 88, 186);
  color: rgb(130, 88, 186);
}

.login-background {
  background-image: linear-gradient(to bottom right, rgb(114, 135, 254), rgb(130, 88, 186));
  height: 100vh;
  margin: 0;
  padding: 0;
  overflow: hidden;
}

/* Login.vue 组件内样式 */
.login-container {
  display: flex;
  width: 60%; /* 调整宽度 */
  max-width: 800px; /* 设置最大宽度 */
  height: auto; /* 高度设为自动，允许内容决定高度 */
  margin: 0 auto;
  margin-top: 10%;
  border-radius: 15px;
  box-shadow: 0 10px 50px 0px rgb(59, 45, 159);
  background-color: rgb(95, 76, 194);
  flex-wrap: wrap; /* 允许子项换行 */
}

.left-container,
.right-container {
  flex: 1;
  min-width: 250px; /* 调整最小宽度 */
  max-width: 50%;
  height: auto;
  padding: 40px; /* 调整内边距 */
  box-sizing: border-box; /* 包括内边距和边框在内计算宽度 */
}

.left-container {
  border-top-left-radius: 15px;
  border-bottom-left-radius: 15px;
  background-image: linear-gradient(to bottom right, rgb(118, 76, 163), rgb(92, 103, 211));
}

.right-container {
  margin-left: 20px; /* 调整左边距 */
  border-radius: 10px;
  background-color: rgb(95, 76, 194);
}

.title {
  color: #fff;
  font-size: 18px;
  font-weight: 200;
}

.title span {
  border-bottom: 3px solid rgb(237, 221, 22);
}

.input-container {
  padding: 20px 0; /* 调整内边距 */
}

input {
  border: 0;
  background: none;
  outline: none;
  color: #fff;
  margin: 10px 0; /* 调整外边距 */
  display: block;
  width: 100%;
  padding: 5px 0;
  transition: 0.2s;
  border-bottom: 1px solid rgb(199, 191, 219);
}

input:hover {
  border-bottom-color: #fff;
}

::-webkit-input-placeholder {
  color: rgb(199, 191, 219);
}

.message-container {
  font-size: 14px;
  transition: 0.2s;
  color: rgb(199, 191, 219);
  cursor: pointer;
}

.message-container:hover {
  color: #fff;
}

.regist-container {
  font-size: 10px;
  color: #fff;
  text-align: center;
  position: relative;
  top: 20px;
}

.regist-container button, .action-container button {
  padding: 10px; /* 调整按钮内边距 */
  display: inline;
  line-height: 20px;
  border-radius: 20px;
  position: absolute;
  bottom: 10px;
  left: calc(50% - 20px);
  transition: 0.2s;
  cursor: pointer;
  background: transparent;
  color: inherit;
}

.regist-container button:hover, .action-container button:hover {
  background-color: grey;
  color: rgb(95, 76, 194);
}

/* 使用媒体查询调整在小屏幕上的样式 */
@media (max-width: 768px) {
  .login-container {
    flex-direction: column; /* 在小屏幕上改为垂直排列 */
    align-items: center; /* 居中对齐 */
  }

  .left-container,
  .right-container {
    width: 100%; /* 宽度改为 100% */
    max-width: none; /* 取消最大宽度限制 */
    height: auto; /* 高度设为自动 */
    margin-left: 0; /* 取消右容器的左边距 */
    margin-bottom: 20px; /* 在小屏幕上增加底部间距 */
  }

  .custom-button {
    width: 100%; /* 按钮宽度在小屏幕上改为 100% */
  }

  .header-title {
    font-size: 50px; /* 调整标题大小 */
  }

  .header-explain {
    font-size: 16px; /* 调整解释文字大小 */
  }
}

</style>
